package winTime.process.source;

import java.io.BufferedReader;
import java.io.FileReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import winTime.Start;
import winTime.vo.tickData;

/**
 * @author fatsung
 * 讀取期交所資料,將資料拆解,存入queue之中
 *
 */
public class FutFileHandler extends Thread{
	private static final Logger log=LoggerFactory.getLogger(FutFileHandler.class);
	private static int sumQty=0;
	@Override
	public void run(){
		log.trace("程式啟動");
		String str="";
		BufferedReader br=null;
		int cnt=0;
		try{
			br = new BufferedReader(new FileReader("C:/java/data/Daily_"+Start.dateTag+".rpt"));
			while(true){
				str = br.readLine();
				cnt++;
				if(str==null){
					break;
				}
				//39669, 1月份, 31210-3月份, 
				if(str.indexOf(",TX")<0){
					continue;
				}
				String a[]=str.split("[,]");
//				log.trace("a[2]:{}, smonth:{}",a[2],Start.smonth);
				if(!a[2].trim().equalsIgnoreCase(Start.smonth)){
					continue;
				}
				String sTime,cTime,price,qty,sqty;
				sTime = a[3];
				cTime = a[3];
				price = a[4];
				qty = ""+(Integer.parseInt(a[5])/2);
				sumQty = sumQty + Integer.parseInt(a[5]) / 2;
				sqty = ""+sumQty;
				Start.put(createBean(sTime,cTime,price,qty,sqty));
			}
		}catch(Exception e){
			log.error("error:{}",e);
		}		
		log.trace("讀取結束");
	}
	private tickData createBean(String sTime,String cTime,String price,String qty,String sqty){
		tickData p=new tickData();
		p.setsTime(Integer.parseInt(sTime));//系統時間
		p.settTime(Integer.parseInt(cTime));//成交時間
		p.setPrice(Integer.parseInt(price));//成交價格
		p.setQty(Integer.parseInt(qty));//成交數量
		p.setSumqty(Integer.parseInt(sqty));//累積數量
		return p;
	}
}
